home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume14 / rast < prev    next >
Encoding:
Internet Message Format  |  1988-05-08  |  17.2 KB

  1. Subject:  v14i086:  Sun rasterfile I/O library
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by: Marc Majka <majka@grads.cs.ubc.cdn>
  7. Posting-number: Volume 14, Issue 86
  8. Archive-name: rast
  9.  
  10. Here is a set of library subroutines which I have found useful for reading
  11. and writing SUN rasterfiles.  The package contains all the usual stuff:
  12. README, Makefile, sources, manuals, and a couple of sample programs.
  13.  
  14. I have tested and debugged the library to some extent, but local hardware
  15. limitations precluded my from giving them a vigorous workout.  There may
  16. be bugs.  If you find or fix any, please let me know!
  17.  
  18. ---
  19. Marc Majka
  20.  
  21. #! /bin/sh
  22. # This is a shell archive, meaning:
  23. # 1. Remove everything above the #! /bin/sh line.
  24. # 2. Save the resulting text in a file.
  25. # 3. Execute the file with /bin/sh (not csh) to create the files:
  26. #    README
  27. #    Makefile
  28. #    rast.c
  29. #    rast.h
  30. #    rast.3
  31. #    rdemo.c
  32. #    rsee.c
  33. export PATH; PATH=/bin:$PATH
  34. if test -f 'README'
  35. then
  36.     echo shar: will not over-write existing file "'README'"
  37. else
  38. cat << \SHAR_EOF > 'README'
  39. This package contains a library of routines for reading and writing
  40. SUN rasterfiles in a pixel-at-a-time manner.  The routines maintain
  41. a data structure called a RASTER, which contains all the usual file
  42. header information from a rasterfile, along with a file pointer and
  43. a memory cache of a single line (row) of the rasterfile.
  44.  
  45. Bit stuffing and unpacking is accomplished with pixrect operations,
  46. which are about as fast as possible.
  47.  
  48. Manifest:
  49.  
  50. rast.h   - header file to include
  51. rast.c   - library source
  52. rast.3   - manual entry
  53. rdemo.c  - demo program
  54. rsee.c   - program to print rasterfile headers
  55. Makefile - to compile it all
  56.  
  57. Note that, since the library pixrect operations, you must include
  58. -lpixrect in any programs which use the library.
  59.  
  60. ---
  61. Marc Majka  -  UBC Laboratory for Computational Vision
  62.  
  63. <majka@vision.ubc.cdn>
  64. <majka@ubc.bitnet>
  65. <majka@ubc-vision.uucp>
  66.  
  67. SHAR_EOF
  68. fi # end of overwriting check
  69. if test -f 'Makefile'
  70. then
  71.     echo shar: will not over-write existing file "'Makefile'"
  72. else
  73. cat << \SHAR_EOF > 'Makefile'
  74. all: rast.o rdemo rsee
  75.  
  76. rast.o: rast.c rast.h
  77.     cc -c rast.c
  78.  
  79. rdemo: rdemo.c rast.o
  80.     cc -o rdemo rdemo.c rast.o -lpixrect
  81.  
  82. rsee: rsee.c rast.o
  83.     cc -o rsee rsee.c rast.o -lpixrect
  84. SHAR_EOF
  85. fi # end of overwriting check
  86. if test -f 'rast.c'
  87. then
  88.     echo shar: will not over-write existing file "'rast.c'"
  89. else
  90. cat << \SHAR_EOF > 'rast.c'
  91. #include "rast.h"
  92.  
  93. RASTER *Ropen(f,m)
  94. char *f;
  95. int m;
  96. {
  97.     RASTER *im, *Ropen_read(), *Ropen_write();
  98.  
  99.     im = NULL;
  100.     if (m == R) im = Ropen_read(f);
  101.     if (m == W) im = Ropen_write(f);
  102.     return(im);
  103. }
  104.  
  105. RASTER *Rdopen(f,m)
  106. int f,m;
  107. {
  108.     RASTER *im, *Rdopen_stdin(), *Rdopen_stdout();
  109.  
  110.     im = NULL;
  111.     if (f == 0 && m == R) im = Rdopen_stdin();
  112.     if (f == 1 && m == W) im = Rdopen_stdout();
  113.     return(im);
  114. }
  115.  
  116. RASTER *Ropen_read(f)
  117. char *f;
  118. {
  119.     RASTER *im;
  120.     char *malloc();
  121.  
  122.     im = (RASTER *)malloc(sizeof(RASTER));
  123.     im->fp = fopen(f,"r");
  124.     im->mode = R;
  125.     if (im->fp == NULL) {
  126.         fprintf(stderr,"read open fails for file \"%s\"!\n",f);
  127.         im = NULL;
  128.         return(im);
  129.     }
  130.     return(im);
  131. }
  132.  
  133. RASTER *Rdopen_stdin()
  134. {
  135.     RASTER *im;
  136.     char *malloc();
  137.  
  138.     im = (RASTER *)malloc(sizeof(RASTER));
  139.     im->fp = stdin;
  140.     im->mode = R;
  141.     if (im->fp == NULL) {
  142.         fprintf(stderr,"read open fails for stdin!\n");
  143.         im = NULL;
  144.         return(im);
  145.     }
  146.     return(im);
  147. }
  148.  
  149. RASTER *Ropen_write(f)
  150. char *f;
  151. {
  152.     RASTER *im;
  153.     char *malloc();
  154.  
  155.     im = (RASTER *)malloc(sizeof(RASTER));
  156.     im->fp = fopen(f,"w");
  157.     if (im->fp == NULL) {
  158.         fprintf(stderr,"write open fails for file \"%s\"!\n",f);
  159.         im = NULL;
  160.         return(im);
  161.     }
  162.  
  163.     im->mode      = W;
  164.     im->width     = 0;
  165.     im->height    = 0;
  166.     im->depth     = 0;
  167.     im->maplength = 0;
  168.     im->maptype   = RMT_NONE;
  169.  
  170.     if (im->fp == NULL) im = NULL;
  171.     return(im);
  172. }
  173.  
  174. RASTER *Rdopen_stdout()
  175. {
  176.     RASTER *im;
  177.     char *malloc();
  178.  
  179.     im = (RASTER *)malloc(sizeof(RASTER));
  180.     im->fp = stdout;
  181.     if (im->fp == NULL) {
  182.         fprintf(stderr,"write open fails for stdout!\n");
  183.         im = NULL;
  184.         return(im);
  185.     }
  186.  
  187.     im->mode      = W;
  188.     im->width     = 0;
  189.     im->height    = 0;
  190.     im->depth     = 0;
  191.     im->maplength = 0;
  192.     im->maptype   = RMT_NONE;
  193.  
  194.     return(im);
  195. }
  196.  
  197. Rgetheader(im)
  198. RASTER *im;
  199. {
  200.     char *malloc();
  201.     int i, rc, ll, extra;
  202.     struct rasterfile rhead;
  203.  
  204.     rc = fread(&rhead,(sizeof (struct rasterfile)),1,im->fp);
  205.     
  206.     if (rc <= 0) {
  207.         fprintf(stderr,"Rgetheader failed!\n");
  208.         return(0);
  209.     }
  210.     if (rhead.ras_magic != RAS_MAGIC) return(0);
  211.  
  212.     im->height    = rhead.ras_height;
  213.     im->width     = rhead.ras_width;
  214.     im->depth     = rhead.ras_depth;
  215.     im->length    = rhead.ras_length;
  216.     im->type      = rhead.ras_type;
  217.     im->maptype   = rhead.ras_maptype;
  218.     im->maplength = rhead.ras_maplength;
  219.  
  220.     extra = (16 - ((im->width * im->depth) % 16)) % 16;
  221.     ll = ((im->width * im->depth) + extra) / 8;
  222.  
  223.     im->linelen   = ll;
  224.     im->cache     = malloc(ll);
  225.     im->c_pixel   = im->width + 1;
  226.     im->c_pr      = mem_point(im->width,1,im->depth,im->cache);
  227.     if (im->maplength > 0) {
  228.         im->map = malloc(im->maplength);
  229.         for (i = 0; i < im->maplength; i++) im->map[i] = getc(im->fp);
  230.     }
  231.     return(1);
  232. }
  233.  
  234. Rputheader(im)
  235. RASTER *im;
  236. {
  237.     char *malloc();
  238.     int i, rc, ll, extra;
  239.     struct rasterfile rhead;
  240.  
  241.     extra = (16 - ((im->width * im->depth) % 16)) % 16;
  242.     ll = ((im->width * im->depth) + extra) / 8;
  243.  
  244.     if (im->width == 0) {
  245.         fprintf(stderr,"rasterfile width is zero!\n");
  246.         return(0);
  247.     }
  248.     if (im->height == 0) {
  249.         fprintf(stderr,"rasterfile height is zero!\n");
  250.         return(0);
  251.     }
  252.     if (im->depth == 0) {
  253.         fprintf(stderr,"rasterfile depth is zero!\n");
  254.         return(0);
  255.     }
  256.  
  257.     rhead.ras_magic     = RAS_MAGIC;
  258.     rhead.ras_width     = im->width;
  259.     rhead.ras_height    = im->height;
  260.     rhead.ras_depth     = im->depth;
  261.     rhead.ras_length    = ll * im->height;
  262.     rhead.ras_type      = RT_STANDARD;
  263.     rhead.ras_maptype   = im->maptype;
  264.     rhead.ras_maplength = im->maplength;
  265.  
  266.     im->linelen   = ll;
  267.     im->cache     = malloc(ll);
  268.     im->c_pixel   = 0;
  269.     im->c_pr      = mem_point(im->width,1,im->depth,im->cache);
  270.  
  271.     rc = fwrite(&rhead,(sizeof (struct rasterfile)),1,im->fp);
  272.     
  273.     if (rc <= 0) {
  274.         fprintf(stderr,"Rputheader failed!\n");
  275.         return(0);
  276.     }
  277.  
  278.     if (im->maptype != RMT_NONE && im->maplength > 0)
  279.         fwrite(im->map,im->maplength,1,im->fp);
  280.     return(1);
  281. }
  282.  
  283. Rgetpix(im)
  284. RASTER *im;
  285. {
  286.     int pix;
  287.     
  288.     if (im->mode != R) {
  289.         fprintf(stderr,"attempt to read rasterfile opened for write!\n");
  290.         return(-1);
  291.     }
  292.  
  293.     if (im->c_pixel >= im->width) {
  294.         fread(im->cache,im->linelen,1,im->fp);
  295.         im->c_pixel = 0;
  296.     }
  297.  
  298.     pix = pr_get(im->c_pr,im->c_pixel,0);
  299.     im->c_pixel++;
  300.     return(pix);
  301. }
  302.  
  303. Rputpix(im,pix)
  304. RASTER *im;
  305. int pix;
  306. {
  307.     if (im->mode != W) {
  308.         fprintf(stderr,"attempt to write rasterfile opened for read!\n");
  309.         return(-1);
  310.     }
  311.  
  312.     pr_put(im->c_pr,im->c_pixel,0,pix);
  313.     im->c_pixel++;
  314.     
  315.     if (im->c_pixel >= im->width) {
  316.         fwrite(im->cache,im->linelen,1,im->fp);
  317.         im->c_pixel = 0;
  318.     }
  319. }
  320.  
  321. Rclose(im)
  322. RASTER *im;
  323. {
  324.     if (im->mode == W) fflush(im->fp);
  325.     fclose(im->fp);
  326. }
  327.  
  328. Rinfo(im)
  329. RASTER *im;
  330. {
  331.     fprintf(stderr,"  height    = %d\n",im->height);
  332.     fprintf(stderr,"  width     = %d\n",im->width);
  333.     fprintf(stderr,"  depth     = %d\n",im->depth);
  334.     fprintf(stderr,"  length    = %d\n",im->length);
  335.     fprintf(stderr,"  type      = %d\n",im->type);
  336.     fprintf(stderr,"  maptype   = %d\n",im->maptype);
  337.     fprintf(stderr,"  maplength = %d\n",im->maplength);
  338. }
  339.  
  340. Rinitmap(im,t,l)
  341. RASTER *im;
  342. int t,l;
  343. {
  344.     char *malloc();
  345.  
  346.     im->maptype   = t;
  347.     im->maplength = l;
  348.     im->map       = malloc(l);
  349. }
  350.  
  351. Rputmap(im,i,r,g,b)
  352. RASTER *im;
  353. int i,r,g,b;
  354. {
  355.     int x;
  356.     
  357.     switch (im->maptype) {
  358.         case RMT_NONE: 
  359.             fprintf(stderr,"can't put in a null map!\n");
  360.             break;
  361.         case RMT_RAW:
  362.             im->map[i] = r;
  363.             break;
  364.         case RMT_EQUAL_RGB:
  365.             x = i * 3;
  366.             im->map[x++] = r;
  367.             im->map[x++] = g;
  368.             im->map[x]   = b;
  369.             break;
  370.         default:
  371.             fprintf(stderr,"unknown map type in rasterfile!\n");
  372.     }
  373. }
  374.  
  375. Rgetmap(im,i,r,g,b)
  376. RASTER *im;
  377. int i,*r,*g,*b;
  378. {
  379.     int x;
  380.     
  381.     switch (im->maptype) {
  382.         case RMT_NONE: 
  383.             fprintf(stderr,"can't get from a null map!\n");
  384.             break;
  385.         case RMT_RAW:
  386.             *r = im->map[i];
  387.             break;
  388.         case RMT_EQUAL_RGB:
  389.             x = i * 3;
  390.             *r = im->map[x++];
  391.             *g = im->map[x++];
  392.             *b = im->map[x];
  393.             break;
  394.         default:
  395.             fprintf(stderr,"unknown map type in rasterfile!\n");
  396.     }
  397. }
  398.  
  399. Rgetmappedpix(im,r,g,b)
  400. RASTER *im;
  401. int *r,*g,*b;
  402. {
  403.     int pix, x;
  404.  
  405.     if (im->mode != R) {
  406.         fprintf(stderr,"attempt to read rasterfile opened for write!\n");
  407.         return(-1);
  408.     }
  409.     
  410.     if (im->c_pixel >= im->width) {
  411.         fread(im->cache,im->linelen,1,im->fp);
  412.         im->c_pixel = 0;
  413.     }
  414.  
  415.     pix = pr_get(im->c_pr,im->c_pixel,0);
  416.     im->c_pixel++;
  417.     
  418.     switch (im->maptype) {
  419.         case RMT_RAW: 
  420.             *r = im->map[pix];
  421.             break;
  422.         case RMT_EQUAL_RGB:
  423.             x = pix * 3;
  424.             *r = im->map[x++];
  425.             *g = im->map[x++];
  426.             *b = im->map[x];
  427.             break;
  428.         default:
  429.             *r = pix;
  430.             break;
  431.     }
  432. }
  433. SHAR_EOF
  434. fi # end of overwriting check
  435. if test -f 'rast.h'
  436. then
  437.     echo shar: will not over-write existing file "'rast.h'"
  438. else
  439. cat << \SHAR_EOF > 'rast.h'
  440. #include <stdio.h>
  441. #include <pixrect/pixrect_hs.h>
  442.  
  443. #define R 0
  444. #define W 1
  445.  
  446. typedef struct RASTER_STRUCT {
  447.     int height;                /* number of rows                            */
  448.     int width;                /* number of columns                        */
  449.     int depth;                /* bits per pixel                            */
  450.     int length;                /* bytes of data following header            */
  451.     int type;                /* rasterfile type                            */
  452.     int maptype;            /* type of lookup table                        */
  453.     int maplength;            /* length of lookup table                    */
  454.     char *map;                /* lookup table                                */
  455.     int linelen;            /* bytes in a row (padded to even 16 bits)    */
  456.     FILE *fp;                /* file pointer                                */
  457.     int mode;                /* read (R) or write (W) mode flag            */
  458.     char *cache;            /* cache of 1 row                            */
  459.     int c_pixel;            /* current pixel in row cache                */
  460.     struct pixrect *c_pr;    /* make cache look like a pixrect            */
  461. } RASTER;
  462. SHAR_EOF
  463. fi # end of overwriting check
  464. if test -f 'rast.3'
  465. then
  466.     echo shar: will not over-write existing file "'rast.3'"
  467. else
  468. cat << \SHAR_EOF > 'rast.3'
  469. .TH RAST 3
  470. .SH NAME
  471. rast - SUN rasterfile interface
  472. .SH SYNOPSIS
  473. .B #include \*(lqrast.h\*(rq
  474. .sp
  475. .B RASTER *Ropen(filename,mode)
  476. .br
  477. .B char *filename;
  478. .br
  479. .B int mode;
  480. .sp
  481. .B RASTER Rdopen(filedes,mode)
  482. .br
  483. .B int filedes, mode;
  484. .sp
  485. .B Rgetheader(raster)
  486. .br
  487. .B RASTER *raster;
  488. .sp
  489. .B Rputheader(raster)
  490. .br
  491. .B RASTER *raster;
  492. .sp
  493. .B Rgetpix(raster)
  494. .br
  495. .B RASTER *raster;
  496. .sp
  497. .B Rputpix(raster,pixel)
  498. .br
  499. .B RASTER *raster;
  500. .br
  501. .B int pixel;
  502. .sp
  503. .B Rgetmappedpix(raster,value)
  504. .br
  505. .B RASTER *raster;
  506. .br
  507. .B int *value;
  508. .sp
  509. .B Rgetmappedpix(raster,red,green,blue)
  510. .br
  511. .B RASTER *raster;
  512. .br
  513. .B int *red, *green, *blue;
  514. .sp
  515. .B Rinitmap(raster,type,length)
  516. .br
  517. .B RASTER *raster;
  518. .br
  519. .B int type, length;
  520. .sp
  521. .B Rputmap(raster,index,value)
  522. .br
  523. .B RASTER *raster;
  524. .br
  525. .B int index, value;
  526. .sp
  527. .B Rputmap(raster,index,red,green,blue)
  528. .br
  529. .B RASTER *raster;
  530. .br
  531. .B int index, red, green, blue;
  532. .sp
  533. .B Rgetmap(raster,index,value)
  534. .br
  535. .B RASTER *raster;
  536. .br
  537. .B int index, *value;
  538. .sp
  539. .B Rgetmap(raster,index,red,green,blue)
  540. .br
  541. .B RASTER *raster;
  542. .br
  543. .B int index, *red, *green, *blue;
  544. .sp
  545. .B Rclose(raster)
  546. .br
  547. .B RASTER *raster;
  548. .sp
  549. .SH DESCRIPTION
  550. This library provides pixel I/O for SUN rasterfiles. 
  551. .sp
  552. .I Ropen
  553. opens a SUN rasterfile for reading or writing, and returns a RASTER
  554. structure.  The mode should be R for read, W for write.  R and W are
  555. defined in the file rast.h.
  556. .I Rdopen
  557. opens a stream, specified by a UNIX file descriptor.  The RASTER structure
  558. is defined below.
  559. .sp
  560. After a rasterfile has been opened to read, data from the rasterfile
  561. header is fetched and placed in the RASTER structure with a call to
  562. .I Rgetheader.
  563. A rasterfile opened for output must have (at least) it's width, height, and
  564. depth set before the header may be written with
  565. .I Rputheader.
  566. If the header is to include a map, the map must be initialized with a call to 
  567. .I Rinitmap.
  568. Map values may be set directly in the RASTER structure, or they my be set
  569. using 
  570. .I Rputmap.
  571. The map type must be one defined in the include file <rasterfile.h>.
  572. .sp
  573. For convenience, map values may be retrieved with
  574. .I Rgetmap.
  575. .sp
  576. Each call to
  577. .I Rgetpix
  578. will return a raw pixel value from the rasterfile. 
  579. .I Rgetmappedpix
  580. returns the result of putting the raw pixel value through the rasterfile map.
  581. .I Rputpix
  582. writes pixel values to the rasterfile.
  583. .sp
  584. A rasterfile is closed with 
  585. .I Rclose.
  586. .SH "RASTER STRUCTURE"
  587. The data structure maintained by these routines is defined in the file rast.h.
  588. .sp
  589. .nf
  590. typedef struct RASTER_STRUCT {
  591.     int height;             /* number of rows                           */
  592.     int width;              /* number of columns                        */
  593.     int depth;              /* bits per pixel                           */
  594.     int length;             /* bytes of data following header           */
  595.     int type;               /* rasterfile type                          */
  596.     int maptype;            /* type of lookup table                     */
  597.     int maplength;          /* length of lookup table                   */
  598.     char *map;              /* lookup table                             */
  599.     int linelen;            /* bytes in a row (padded to even 16 bits)  */
  600.     FILE *fp;               /* file pointer                             */
  601.     int mode;               /* read (R) or write (W) mode flag          */
  602.     char *cache;            /* cache of 1 row                           */
  603.     int c_pixel;            /* current pixel in row cache               */
  604.     struct pixrect *c_pr;   /* make cache look like a pixrect           */
  605. } RASTER;
  606. .fi
  607. .sp
  608. The height, width, depth, length, type, maptype, and maplength fields
  609. have exactly the same meaning as they do in rasterfiles.  The map field
  610. is used to store the rasterfile map data.
  611. .sp
  612. The linelen field gives the number of bytes in a single line (row)
  613. in the rasterfile.  This is different from the width because each line
  614. is padded with zero bits to make it an integer multiple of 16 bits in length.
  615. .sp
  616. The UNIX file pointer is kept in the fp field, and a read/write flag is 
  617. kept in the mode field.
  618. .sp
  619. Pixels are stored in the cache field, which holds one line (row) of the
  620. raster. The cache is maintained by 
  621. .I Rgetpix
  622. and
  623. .I Rputpix.
  624. The current pixel (column) within the cache is recorded by in the c_pixel
  625. field.  The cache is efficiently accessed by turning it into a memory pixrect
  626. and using 
  627. .I pr_get
  628. and
  629. .I pr_put
  630. to do the necessary bit stuffing.  The memory pixrect is kept in the c_pr
  631. field.  Because of this, programs using this library must be compiled with
  632. -lpixrect.
  633. .SH AUTHOR
  634. Marc Majka
  635. SHAR_EOF
  636. fi # end of overwriting check
  637. if test -f 'rdemo.c'
  638. then
  639.     echo shar: will not over-write existing file "'rdemo.c'"
  640. else
  641. cat << \SHAR_EOF > 'rdemo.c'
  642. /***********************************************************************
  643.  
  644.    rdemo - reads a rasterfile, computes the average of the colour
  645.    values (if applicable) of the pixels, and writes a new rasterfile
  646.    with the average.  An inverting colour map is added.
  647.  
  648. ***********************************************************************/
  649.  
  650. #include <stdio.h>
  651. #include "rast.h"
  652.  
  653. main(argc,argv)
  654. int argc;
  655. char *argv[];
  656. {
  657.     int r, g, b, avg, row, col, i, pix, ifile, ofile;
  658.     RASTER *irast, *orast, *Ropen(), *Rdopen();
  659.  
  660.     ifile = 0;
  661.     ofile = 0;
  662.  
  663.     for (i = 1; i < argc; i++) {
  664.         if (argv[i][0] == '-') {
  665.             switch (argv[i][1]) {
  666.                 case 'i': ifile = ++i; break;
  667.                 case 'o': ofile = ++i; break;
  668.                 default:  usage();
  669.             }
  670.         }
  671.     }
  672.  
  673.     if (ifile) irast = Ropen(argv[ifile],R);
  674.     else irast = Rdopen(0,R);
  675.     if (irast == NULL) {
  676.         fprintf(stderr,"rdemo: input rasterfile open failed!\n");
  677.         exit(1);
  678.     }
  679.  
  680.     if (ofile) orast = Ropen(argv[ofile],W);
  681.     else orast = Rdopen(1,W);
  682.     if (irast == NULL) {
  683.         fprintf(stderr,"rdemo: output rasterfile open failed!\n");
  684.         exit(1);
  685.     }
  686.  
  687.     if (!Rgetheader(irast)) {
  688.         fprintf(stderr,"input file is not a raster file!\n");
  689.         exit(1);
  690.     }
  691.  
  692.     orast->height = irast->height;
  693.     orast->width  = irast->width;
  694.     orast->depth  = irast->depth;
  695.  
  696.     Rinitmap(orast,RMT_EQUAL_RGB,256*3);
  697.     for (i = 0; i < 256; i++) Rputmap(orast,255-i,i,i,i);
  698.     
  699.     Rputheader(orast);
  700.  
  701.     if (irast->maptype == RMT_EQUAL_RGB) {
  702.         for (row = 0; row < irast->height; row++) 
  703.             for (col = 0; col < irast->width; col++) {
  704.                 Rgetmappedpix(irast,&r,&g,&b);
  705.                 avg = (r + g + b) / 3;
  706.                 Rputpix(orast,avg);
  707.             }
  708.     }
  709.     else if (irast->maptype == RMT_RAW) {
  710.         for (row = 0; row < irast->height; row++) 
  711.             for (col = 0; col < irast->width; col++) {
  712.                 Rgetmappedpix(irast,&pix);
  713.                 Rputpix(orast,pix);
  714.             }
  715.     }
  716.     else if (irast->maptype == RMT_NONE) {
  717.         for (row = 0; row < irast->height; row++) 
  718.             for (col = 0; col < irast->width; col++) {
  719.                 pix = Rgetpix(irast);
  720.                 Rputpix(orast,pix);
  721.             }
  722.     }
  723.     else {
  724.         fprintf(stderr,"rdemo: unknown map type in input rasterfile!\n");
  725.         exit(1);
  726.     }
  727.     
  728.     Rclose(irast);
  729.     Rclose(orast);
  730. }
  731.  
  732. usage()
  733. {
  734.     fprintf(stderr,"usage: rdemo [-i file] [-o file]\n");
  735.     fprintf(stderr,"       -i  input rasterfile [default is stdin]\n");
  736.     fprintf(stderr,"       -o  output rasterfile [default is stdout]\n");
  737.     exit(0);
  738. }
  739. SHAR_EOF
  740. fi # end of overwriting check
  741. if test -f 'rsee.c'
  742. then
  743.     echo shar: will not over-write existing file "'rsee.c'"
  744. else
  745. cat << \SHAR_EOF > 'rsee.c'
  746. #include <stdio.h>
  747. #include "rast.h"
  748.  
  749. main(argc,argv)
  750. int argc;
  751. char *argv[];
  752. {
  753.     RASTER *irast, *Ropen();
  754.     int i, rc;
  755.  
  756.     for (i = 1; i < argc; i++) {
  757.         irast = Ropen(argv[i],R);
  758.         if (irast == NULL)
  759.             fprintf(stderr,"rasterfile open failed for \"%s\"!\n",argv[i]);
  760.         else {
  761.             rc = Rgetheader(irast);
  762.             fprintf(stderr,"%s:\n",argv[i]);
  763.             if (rc) Rinfo(irast);
  764.             else fprintf(stderr,"not a rasterfile!\n");
  765.             fprintf(stderr,"\n");
  766.             Rclose(irast);
  767.         }
  768.     }
  769.     exit(0);
  770. }
  771. SHAR_EOF
  772. fi # end of overwriting check
  773. #    End of shell archive
  774. exit 0
  775.  
  776.  
  777.